package com.hjb.springboot.elasticsearch.service;


import com.hjb.springboot.elasticsearch.entity.City;
import com.hjb.springboot.elasticsearch.repository.CityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;

import org.springframework.data.domain.Page;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;

import static org.elasticsearch.index.query.QueryBuilders.matchQuery;

@Service
@Primary
public class CityService {

    @Autowired
     private CityRepository cityrepository;

     public Long save(City city){

         City city1 = cityrepository.save(city);
         return  city1.getId();
     }
     public Page<City> query(String querycontent){
         SearchQuery searchQuery = new NativeSearchQueryBuilder()
                 .withQuery(matchQuery("name",querycontent)).build();
         return cityrepository.search(searchQuery);
     }
}
